package com.example.foo.admin.web.product;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.ruoyi.common.core.domain.AjaxResult;
import com.example.core.PageResult;
import com.example.core.Pageable;
import com.example.foo.product.model.Item;
import com.example.foo.product.query.ItemQuery;
import com.example.foo.product.service.ItemService;

@org.springframework.web.bind.annotation.RestController
public class ItemAction {

	private final ItemService itemService;

	public ItemAction(ItemService itemService) {
		this.itemService = itemService;
	}

	@GetMapping("/api/items")
	public AjaxResult index(ItemQuery itemQuery, Pageable pageable) {
		PageResult<Item> pageResult = itemService.findItems(itemQuery, pageable);
		return AjaxResult.successData(pageResult);
	}

	@GetMapping("/api/items/{id}")
	public AjaxResult showItem(@PathVariable("id") Long id) {
		Item item = itemService.findItemById(id);
		if (item == null) {
			return AjaxResult.error(404, "Not Found");
		}
		return AjaxResult.successData(item);
	}

	@PostMapping("/api/items")
	public AjaxResult createItem(Item itemForm) {
		AjaxResult validateError = validateForm(itemForm);
		if (validateError != null) {
			return validateError;
		}

		Item item = new Item();
		item.setType(itemForm.getType());
		item.setName(itemForm.getName());
		item.setPicUrl(itemForm.getPicUrl());
		item.setPicUrlJson(itemForm.getPicUrlJson());
		item.setUnitPrice(itemForm.getUnitPrice());
		item.setOriginalPrice(itemForm.getOriginalPrice());
		item.setBody(itemForm.getBody());
		itemService.createItem(item);

		return AjaxResult.success();
	}

	@PutMapping("/api/items/{id}")
	public AjaxResult updateItem(@PathVariable("id") Long id, Item itemForm) {
		AjaxResult validateError = validateForm(itemForm);
		if (validateError != null) {
			return validateError;
		}

		Item item = itemService.findItemById(id);
		if (item == null) {
			return AjaxResult.error(404, "Not Found");
		}

		item.setType(itemForm.getType());
		item.setName(itemForm.getName());
		item.setPicUrl(itemForm.getPicUrl());
		item.setPicUrlJson(itemForm.getPicUrlJson());
		item.setUnitPrice(itemForm.getUnitPrice());
		item.setOriginalPrice(itemForm.getOriginalPrice());
		item.setBody(itemForm.getBody());
		itemService.updateItem(item);

		return AjaxResult.success();
	}

	@PutMapping("/api/items/{id}/state")
	public AjaxResult updateItemState(@PathVariable("id") Long id, @RequestParam("state") Item.State state) {
		if (state == null) {
			return AjaxResult.error(400, "state不能为空");
		}

		Item item = itemService.findItemById(id);
		if (item == null) {
			return AjaxResult.error(404, "Not Found");
		}

		itemService.updateItemState(id, state);

		return AjaxResult.success();
	}

	private AjaxResult validateForm(Item itemForm) {
		if (itemForm.getType() == null) {
			return AjaxResult.error(400, "类型不能为空");
		}
		if (StringUtils.isBlank(itemForm.getName())) {
			return AjaxResult.error(400, "名称不能为空");
		}
		if (StringUtils.isBlank(itemForm.getPicUrl())) {
			return AjaxResult.error(400, "主图不能为空");
		}
		if (StringUtils.isBlank(itemForm.getPicUrlJson())) {
			return AjaxResult.error(400, "多图不能为空");
		}
		if (itemForm.getUnitPrice() == null) {
			return AjaxResult.error(400, "单价不能为空");
		}
		if (itemForm.getOriginalPrice() == null) {
			return AjaxResult.error(400, "原价不能为空");
		}
		if (StringUtils.isBlank(itemForm.getBody())) {
			return AjaxResult.error(400, "文本不能为空");
		}
		return null;
	}

}
